﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace POC_linqSql
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Program started ... ");

            BalderDataContext db = new BalderDataContext();

            #region select
            var movies =
                from m in db.Movies
                select m;
            foreach (var item in movies)
            {
                Console.WriteLine("Movie: {0}",item.Title);
            }
            #endregion

            Console.WriteLine();
            Console.WriteLine();

            #region where
            var bladeMovies =
                from m in db.Movies
                where m.Title.Contains("Blade")
                select m;
            foreach (var item in bladeMovies)
            {
                Console.WriteLine("Movie: {0}",item.Title);
            }
            #endregion

            Console.WriteLine();
            Console.WriteLine();

            #region join/where
            var dieHardActors =
                from ma in db.Movie_Actors
                join a in db.Actors
                    on ma.actorId equals a.id
                join m in db.Movies
                    on ma.movieId equals m.Id
                where m.Title.Equals("Die Hard")
                select a;
            Console.WriteLine("Die Hard actors:");
            foreach (var item in dieHardActors)
            {
                Console.WriteLine("Actor: {0}",item.name);
            }

            Console.WriteLine();
            Console.WriteLine();

            var bladeCinemas =
                from mc in db.Movie_Cinemas
                join m in db.Movies
                    on mc.movieId equals m.Id
                join c in db.Cinemas
                    on mc.cinemaId equals c.id
                where m.Title.Contains("Blade")
                select c;
            Console.WriteLine("Cinemas showing a blade movie:");
            foreach (var item in bladeCinemas)
            {
                Console.WriteLine("Cinema: {0} in {1}",item.name,item.location);
            }
            #endregion

            Console.WriteLine();
            Console.WriteLine();

            #region join/where/distinct
            var bladeCinemasDistinct =
                (from mc in db.Movie_Cinemas
                join m in db.Movies
                    on mc.movieId equals m.Id
                join c in db.Cinemas
                    on mc.cinemaId equals c.id
                where m.Title.Contains("Blade")
                select c).Distinct();
            Console.WriteLine("Cinemas showing a blade movie, distinct:");
            foreach (var item in bladeCinemasDistinct)
            {
                Console.WriteLine("Cinema: {0} in {1}", item.name, item.location);
            }
            #endregion

            Console.WriteLine();
            Console.WriteLine();

            # region update
            Cinema cinema = db.Cinemas.Single(c => c.name == "Biocity" && c.location == "København");
            Console.WriteLine("Cinema: {0} in {1} should change location to Aarhus",cinema.name,cinema.location);

            cinema.location = "Aarhus";

            db.SubmitChanges();

            cinema = db.Cinemas.Single(c => c.name == "Biocity" && c.location == "Aarhus");
            Console.WriteLine("Cinema: {0} in {1}",cinema.name,cinema.location);
            
            Console.WriteLine("Reverting back to København");
            cinema.location = "København";
            db.SubmitChanges();
            #endregion

            Console.WriteLine();
            Console.WriteLine();

            Console.ReadLine();
        }
    }
}
